﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using CrystalDecisions.Web;
using Single_SignOn.Controllers;
using Single_SignOn.Models;

namespace Single_SignOn
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        //protected void Application_Start()
        //{
        //    AreaRegistration.RegisterAllAreas();

        //    WebApiConfig.Register(GlobalConfiguration.Configuration);
        //    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        //    RouteConfig.RegisterRoutes(RouteTable.Routes);
        //}
        //*
        //
        
        
        //*
        
        public void Application_Error(object sender, EventArgs e)
        {

            System.Exception appException = Server.GetLastError();
            if (appException == null) return;
            /**/
            HomeController homeController = new HomeController();
            if ((((System.Web.HttpApplication)(sender)).Request.Url.Segments[1]).Contains("Web"))
            {
                Response.Redirect("~/" + ((System.Web.HttpApplication)(sender)).Request.Url.Segments[1] + "Authentication/Index");
            }
            else if ((((System.Web.HttpApplication)(sender)).Request.Url.Segments[1]).Contains("Handheld"))
            {
                Response.Redirect("~/" + ((System.Web.HttpApplication)(sender)).Request.Url.Segments[1] + "Login/Index");
            }
            else if ((((System.Web.HttpApplication)(sender)).Request.Url.Segments[1]).Contains("PDA"))
            {
                Response.Redirect("~/" + ((System.Web.HttpApplication)(sender)).Request.Url.Segments[1] + "Login/Index");
            }
            /*           
            Server.ClearError();
            Response.Write(appException.Message);
            Response.End();
             */ 
        }

        protected void Application_AcquireRequestState(object sender, EventArgs e)
        {
            CultureInfo ci;
            if (Session["UserInfo"] != null)
            {
                String culture = String.IsNullOrEmpty(((UserProfile) (Session["UserInfo"])).Language)
                    ? "en-US"
                    : ((UserProfile)(Session["UserInfo"])).Language;
                //culture =(culture == "en-EN" ) ? "en-US" : culture; 
                ci = new CultureInfo(culture);
            }
            else
            {
                ci = new CultureInfo("en-US");
            }
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar 
                = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;
            
        }


        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                "Handheld",
                "Handheld/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Web",
                "Web/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
        //*
    }
}